﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VOL;
using BLL;
using System.Data;
using WebShoes.Class;

namespace WebShoes
{
    public partial class ThanhToanForm : System.Web.UI.Page
    {
        private GioHang _GioHang = new GioHang();
        private DataTable _dtGioHang = new DataTable();
        private KhachHangBL _KhachHangBL = new KhachHangBL();
        private HoaDonBL _HoaDonBL = new HoaDonBL();
        int chonThanhPho = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                fillData();
            }
        }

        protected void btnThanhToan_Click(object sender, EventArgs e)
        {
            if (GioHang.CheckTonTai())
            {
                _dtGioHang = (DataTable)HttpContext.Current.Session["cart"];
                //  insert table dmkhach
                KhachHang objKhach = new KhachHang();
                objKhach.HoTenKhachHang = txtHoTen.Text;
                objKhach.DiaChi = txtDiaChi.Text;
                objKhach.DienThoai = txtDienThoai.Text;
                objKhach.DienThoaiDiDong = txtDienThoaiDiDong.Text;
                objKhach.Email = txtEmail.Text;
                objKhach.MaQuanHuyen = 1;
                objKhach.MaThanhPho = 1;
                _KhachHangBL.insertKhachHang(objKhach);
                //objKhach.HoTen = txtHoTen.Text;
                //objKhach.Email = txtEmail.Text;
                //objKhach.DienThoai = txtDienThoai.Text;
                //objKhach.DiaChi = txtDiaChi.Text;
                //_DMKhachBL.insertDMKhach(objKhach);

                // insert table hoadonban
                HoaDon obj = new HoaDon();
                obj.MaKhachHang = _KhachHangBL.GetMaKhachHangNew();
                foreach (DataRow row in _dtGioHang.Rows)
                {
                    obj.MaHangHoa = int.Parse(row["MaHangHoa"].ToString());
                    obj.NgayMua = DateTime.Now;
                    obj.NgayGiaoHang = DateTime.Now;
                    obj.SoLuong = int.Parse(row["SoLuong"].ToString());
                    obj.DonGia = float.Parse(row["DonGia"].ToString());
                    _HoaDonBL.insertHoaDon(obj);
                }
                //obj.MaKhach = _DMKhachBL.GetMaKhachNew();
                //obj.NgHD = DateTime.Now;
                //obj.DienGiai = txtDienGiai.Text;
                //obj.Duyet = false;
                //obj.NgDuyet = DateTime.Now;
                //_HoaDonBanBL.insertHoaDonBan(obj);

                //// insert table chitiethoadon
                //int MaHoaDon = _HoaDonBanBL.GetMaHoaDonNew();
                //foreach (DataRow r in _dtGioHang.Rows)
                //{
                //    ChiTietHoaDon chiTietHoaDon = new ChiTietHoaDon();
                //    chiTietHoaDon.MaHang = int.Parse(r["MaHang"].ToString());
                //    chiTietHoaDon.MaHD = MaHoaDon;
                //    chiTietHoaDon.DonGia = float.Parse(r["DonGia"].ToString());
                //    chiTietHoaDon.SoLuong = int.Parse(r["SoLuong"].ToString());
                //    _ChiTietHoaDonBL.insertChiTietHoaDon(chiTietHoaDon);
                //}

                // xóa session giỏ hàng
                // có thêm chữ static là gọi như thế này
                // không cần phải khởi tạo đối tượng
                GioHang.DeleteGioHang();
                Response.Write("<script LANGUAGE='JavaScript' >alert('Mua hàng thành công !');window.location = 'Default.aspx';</script>");
            }
        }

        private void loadButton()
        {        
            QuanHuyenBL _QuanHuyenBL = new QuanHuyenBL();
            try
            {
                ddlQuanHuyen.DataSource = _QuanHuyenBL.GetDataTP(chonThanhPho);
                ddlQuanHuyen.DataTextField = "TenQuanHuyen";
                ddlQuanHuyen.DataValueField = "MaQuanHuyen";
                ddlQuanHuyen.DataBind();
            }
            catch (Exception ex) { }
        }

        private void fillData()
        {
            if (GioHang.CheckTonTai())
            {
                GridView1.DataSource = GioHang.GetGioHang();
                GridView1.DataBind();

                lblTongTien.Text = _GioHang.TongTien().ToString();

                ThanhPhoBL _ThanhPhoBL = new ThanhPhoBL();
                ddlThanhPho.DataSource = _ThanhPhoBL.GetData();
                ddlThanhPho.DataTextField = "TenThanhPho";
                ddlThanhPho.DataValueField = "MaThanhPho";
                ddlThanhPho.DataBind();
                ddlThanhPho.Items.Insert(0, "--Vui lòng chọn thành phố--");
            }
            
        }

        protected void ddlThanhPho_SelectedIndexChanged(object sender, EventArgs e)
        {
            chonThanhPho = int.Parse(ddlThanhPho.SelectedValue);
            loadButton();
        }
    }
}